const YoloDetector = require('../yolo');
const APP = require('../APP');
class index {
    androidBot = null;
    end = false;
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async isEnd() {
        let data = await YoloDetector.screen(this.androidBot);
        let game_pop_gameOver = APP.findClass_v2(data, 'game_pop_gameOver');
        if (game_pop_gameOver) {
            let btn_text = APP.findClass_v2(data, 'btn_text', '现在退出');
            if(btn_text){
                await btn_text.click();
                return true;
            }
            APP.saveScreenshot_error(this.androidBot,'游戏结束');
        }
        return false;
    }
    async isOuverPage(){
        let data = await YoloDetector.screen(this.androidBot);
        let nextBtn = APP.findClass_v2(data, 'game_tips','下一步');
        let shareBtn = APP.findClass_v2(data, 'game_tips','分享');
        if(nextBtn && shareBtn){
            await nextBtn.click();
            return true;
        }
        return false;
    }
    async run() {
        console.log('开始登陆流程')
        while (!this.end) {
            let isEnd = await this.isEnd();
            if (isEnd) {
                continue;
            }
            let isOuverPage = await this.isOuverPage();
            if(isOuverPage){
                break;
            }
            let data = await YoloDetector.screen(this.androidBot);
            let game_tips = APP.findClass_index(data, 'game_tips');
            if (game_tips) {
                let info = await APP.ocrRegion(this.androidBot, data[game_tips - 1].region);
                if (info) {
                    console.log('游戏提示', info);
                    continue;
                }
            }
            await APP.sleep(1000);
        }
    }
}

module.exports = index;